package com.carlos.grabredenvelope.db

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.CursorFactory
import com.carlos.cutils.thirdparty.database.MigrationHelper
import com.carlos.grabredenvelope.db.DaoMaster.OpenHelper
import org.greenrobot.greendao.AbstractDao

/**
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */

/**
 * Created by Carlos on 2020/3/3.
 */
//link:https://stackoverflow.com/questions/13373170/greendao-schema-update-and-data-migration/30334668#30334668
class UpgradeHelper(
    context: Context?,
    name: String?,
    factory: CursorFactory?,
    vararg daoClasses: Class<out AbstractDao<*, *>?>
//     daoClasses: Class<out AbstractDao<*, *>?>
) : OpenHelper(context, name, factory) {
    private val mClasses: Array<Class<out AbstractDao<*, *>?>>
    init {
        mClasses = daoClasses as Array<Class<out AbstractDao<*, *>?>>
    }

    override fun onUpgrade(
        sqLiteDatabase: SQLiteDatabase,
        oldVersion: Int,
        newVersion: Int
    ) {
        MigrationHelper.migrate(
            sqLiteDatabase,
            *mClasses
        )
    }
}